﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using Joyintech.Common;

namespace Joyintech.Admin.inpiao365.general.system.menu
{
    public partial class left : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            { 
                //绑定菜单树
                DataBindTree("0", this.treeViewMenu.Nodes);
            }
        }

        #region 绑定菜单树

        /// <summary>
        /// 绑定菜单树
        /// </summary>
        /// <param name="menuPID"></param>
        /// <param name="Nds"></param>
        private void DataBindTree(string menuPID, TreeNodeCollection Nds)
        {
            List<systemMenu> list = new List<systemMenu>();

            list = new systemMenu_BLL().GetModelList("menuPID=" + menuPID + " order by menuOrder");

            if (list != null && list.Count > 0)
            {
                foreach (systemMenu model in list)
                {
                    TreeNode treeNode = new TreeNode();
                    treeNode.Text = model.MenuName;
                    treeNode.Value = model.MenuId.ToString();
                    treeNode.ImageUrl = "/theme/read_0.png";
                    treeNode.NavigateUrl = "edit.aspx?menu_id=" + treeNode.Value;

                    Nds.Add(treeNode);

                    //递归查找子部门
                    DataBindTree(treeNode.Value, treeNode.ChildNodes);
                }
            }
        }

        #endregion

        #region 删除选中的菜单

        /// <summary>
        /// 删除选中的菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tns = this.treeViewMenu.CheckedNodes;

            if (tns == null || tns.Count == 0)
            {
                MessageBox.Show(this, "请选择需要删除的菜单！");
                return;
            }

            //操作结果
            int dataResult = 0;

            foreach (TreeNode treeNode in tns)
            {
                dataResult += new systemMenu_BLL().Delete(Convert.ToInt32(treeNode.Value));
            }

            if (dataResult > 0)
            {
                MessageBox.ShowMsgScript(this, "共删除" + dataResult + "个菜单！", "parent.location.href=parent.location.href;");
            }
            else
            {
                MessageBox.Show(this, "操作发生异常，菜单删除失败，请重试！");
            }
        }

        #endregion
    }
}